package edu.cuit.avatar.loop;

import java.util.Scanner;

/**
 * 使用穷举法计算最大公约数
 */
public class GreatestCommonDivisor2 {
    public static void main(String[] args) {
        System.out.println("请输入两个整数:");
        Scanner input = new Scanner(System.in);
        int number1 = input.nextInt(),
            number2 = input.nextInt();

        input.close();

        if(number1 > number2){
            int temp = number1;
            number1 = number2;
            number2 = temp;
        }

        //最大公约数
        int gcd = 1;

        if(number2 % number1 == 0){
            gcd = number1;
            //结束方法的调用,后续逻辑不再执行
            System.out.printf("(%d, %d)的最大公约数:%d", number1, number2, gcd);
            return;
        }

        //从上往下开始迭代
        for(int k = number1 / 2; k >= 2; k--){
            if(number1 % k == 0 && number2 % k == 0){ //最大公约数
                gcd = k;
                //跳出循环,不再继续循环
                break;
            }
        }

        System.out.printf("(%d, %d)的最大公约数:%d", number1, number2, gcd);
    }
}
